/*
 * Copyright (c) 2013, SRI International
 * All rights reserved.
 * Licensed under the The BSD 3-Clause License;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 * 
 * http://opensource.org/licenses/BSD-3-Clause
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of the aic-web-praise nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.sri.ai.lpitools.executionengine.server.components.queryListener;

import java.util.List;

import org.apache.log4j.Logger;

import com.sri.ai.lpitools.executionengine.server.callback.CallBackClient_Interface;
import com.sri.ai.praise.lbp.LBPQueryEngine;
import com.sri.ai.praise.lbp.LBPQueryEngine.QueryStep;

/**
 * This class is the custom implementation of the LBPQueryEngine.QueryStepListener interface
 * It provides the proper call backs for timing and to update gui with the progress report
 * @author pwolf
 *
 */
public class QueryStepListener implements LBPQueryEngine.QueryStepListener{

	private List<Long> timeList;
	private List<String> stepList;
	private CallBackClient_Interface client;
	private String clientID;
	private String decription = "";
	private static final Logger logger = Logger.getLogger(QueryStepListener.class.getName());	
	
	public QueryStepListener(List<Long> timeList, List<String> stepList, CallBackClient_Interface client, String clientID){
		this.timeList = timeList;
		this.client = client;
		this.clientID = clientID;
		this.stepList= stepList;
	}
	
	/**
	 * This function returns the time list after we are done with the query run
	 * @return
	 */
	public List<Long> getTimeList(){
		return timeList;
	}
	
	public List<String> getStepList(){
		return stepList;
	}
	public void queryStepStarting(String queryUUID, String description) {
		logger.trace("Starting: "+description+ " " + queryUUID);	
		this.decription = description;
	}
	

	public void queryStepComplete(String queryUUID,
			QueryStep completedStep) {
		logger.trace("Finished: " + completedStep.getDescription() + " in " + completedStep.getTimeInNanoseconds() + " nanoseconds " + queryUUID);
		processSubStep(queryUUID, completedStep);
		sendUpdate();
	}
	
	private void processSubStep(String queryUUID, QueryStep step){
		if(step.hasSubSteps()){
			List<QueryStep> subSteps = step.getSubSteps();
			for(QueryStep subStep: subSteps){
				logger.trace("    "+subStep.getDescription() + " in " + subStep.getTimeInNanoseconds() + " nanoseconds "+queryUUID);
				processSubStep(queryUUID, subStep);
			}
		}
		else{
			timeList.add( step.getTimeInNanoseconds());
			stepList.add( step.getDescription());
		}
	}
	/**
	 * This function is used to call the client so it will update the gui with step info
	 */
	private void sendUpdate(){
		if(!decription.equalsIgnoreCase("")){
			logger.trace("In update time");
			String step = this.decription.substring(0, 6);
			step = "After " + step;
			logger.trace("In update time before rpc call");
			
			client.updateTime(step, clientID);
		}
	}




}
